from django.contrib import admin
from .models import UserPoints, PointsRecord, Reward, RewardExchange


@admin.register(UserPoints)
class UserPointsAdmin(admin.ModelAdmin):
    list_display = ['user', 'total_points', 'available_points', 'used_points', 'updated_at']
    search_fields = ['user__username']
    readonly_fields = ['total_points', 'used_points']


@admin.register(PointsRecord)
class PointsRecordAdmin(admin.ModelAdmin):
    list_display = ['user', 'points', 'type', 'description', 'created_at']
    list_filter = ['type', 'created_at']
    search_fields = ['user__username', 'description']


@admin.register(Reward)
class RewardAdmin(admin.ModelAdmin):
    list_display = ['name', 'type', 'points_required', 'stock', 'is_active', 'created_at']
    list_filter = ['type', 'is_active', 'created_at']
    search_fields = ['name', 'description']


@admin.register(RewardExchange)
class RewardExchangeAdmin(admin.ModelAdmin):
    list_display = ['user', 'reward', 'points_used', 'status', 'created_at', 'completed_at']
    list_filter = ['status', 'created_at']
    search_fields = ['user__username', 'reward__name']
    
    actions = ['complete_exchanges']
    
    def complete_exchanges(self, request, queryset):
        from django.utils import timezone
        queryset.update(status='completed', completed_at=timezone.now())
        self.message_user(request, f'已完成 {queryset.count()} 个兑换')
    complete_exchanges.short_description = '完成选中的兑换'
